package com.waya.web.browser;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.waya.web.browser.manager.WbWebManager;
import com.waya.web.browser.player.view.MediaPlayerLayout;
import com.waya.web.browser.webview.WebDefaultViewClient;
import com.waye.wanba.utils.ToastUtil;

import java.util.Map;

/**
 * Created by JesseHev on 2018/1/16.
 */
public class WayeWebViewbActivity extends Activity {

    private WebView mWebview;

    private String mUrl;

    private WbWebManager mWebManager;

    private MediaPlayerLayout mediaPlayerLayout;
    private String mVideoUrl = "http://111.161.5.36/h5/Public/Home/video/video_20160212.mp4";

    public static final String KEY_URL = "URL";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        mWebview = (WebView) findViewById(R.id.webView);
        mediaPlayerLayout = (MediaPlayerLayout) findViewById(R.id.play_layout);
        getIntentData();
        mediaPlayerLayout.setWebView(mWebview);
        mWebManager = WbWebManager.getInstance(this);
        mWebManager.setTarget(mWebview)
//                .setCookie(mUrl, "JSEESIONID=0000000000000")
//                .setCookie(mUrl, "jesseid=012145")
                .setSetting()
                .palyerLayout(mediaPlayerLayout)
                .registerJsObj()
                .setChromClient(getWebChromeClient())
                .setWebClient(getWebViewClient())
                .load(mUrl);
    }

    private void getIntentData() {
        mUrl = getIntent().getStringExtra(KEY_URL);

        if (TextUtils.isEmpty(mUrl)) {
            throw new NullPointerException("webview url must not be null...");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebview.canGoBack()) {
                if (mWebview.getUrl().equals(mUrl)) {
                    return super.onKeyDown(keyCode, event);
                } else {
                    mWebview.goBack();
                    return true;

                }
            } else {
                return super.onKeyDown(keyCode, event);
            }
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            mediaPlayerLayout.onError("0", "error==>");
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            mediaPlayerLayout.onCompleted();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_3) {
            mediaPlayerLayout.onExit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private WebChromeClient getWebChromeClient() {
        return new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                System.out.println("progress===>" + newProgress);
                mWebview.setVisibility(View.VISIBLE);
                mWebview.requestFocus();
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                ToastUtil.getInstace().showToastByString(WayeWebViewbActivity.this, message);
                result.confirm();
                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                ToastUtil.getInstace().showToastByString(WayeWebViewbActivity.this, message);
                result.confirm();
                return true;
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                ToastUtil.getInstace().showToastByString(WayeWebViewbActivity.this, message);
                result.confirm();
                return true;
            }
        };
    }

    private WebViewClient getWebViewClient() {
        return new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mWebview.requestFocus();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description,
                                        String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }
        };
    }

    @Override
    protected void onDestroy() {
        mWebManager.destroy();
        super.onDestroy();
        System.exit(0);
    }

}
